package de.infotheo;

import java.math.BigInteger;

/**
 * The class ElGamal contains a method to calculate the clear text of a given
 * ElGamal encrypted text
 * 
 * @author Eugen
 * 
 */
public class ElGamal {

	/**
	 * The method calculates the clear text of the given ElGamal encrypted text
	 * 
	 * @param y1
	 *            chiffre text part 1
	 * @param y2
	 *            chiffre text part 2
	 * @param alpha
	 *            part of public key
	 * @param beta
	 *            part of public key
	 * @param p
	 *            prime modulus as another part of the public key
	 * 
	 * @return clear text
	 */
	public BigInteger decY(BigInteger y1, BigInteger y2, BigInteger alpha,
			BigInteger beta, BigInteger p) {

		// initiation of parameter
		BigInteger cleartext = new BigInteger("0");
		LogG logg = new LogG();

		// calculation of a by determining the discrete logarithm of log_alpha
		// beta mod p
		int a = logg.calcLogG(Integer.parseInt(alpha.toString()),
				Integer.parseInt(beta.toString()),
				Integer.parseInt(p.toString()));

		// calculation of the clear text by multiplying of y2 with y1^(-a)
		cleartext = y2.multiply(y1.modPow(new BigInteger(String.valueOf(a)), p)
				.modInverse(p));

		System.out.println("Cleartext of (" + y1.toString() + ","
				+ y2.toString() + "), where a = " + String.valueOf(a)
				+ ", is: " + cleartext.toString());

		return cleartext;
	}
}
